package com.hgd.XML;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.hgd.ORM.Device;
import com.hgd.ORM.Domain;
import com.hgd.ORM.Task;
import com.hgd.ORM.TaskKind;
import com.thoughtworks.xstream.XStream;

public class TurnXmlToDevice {
	/**
	 * 读取初始化配置文件
	 * @return
	 * @throws IOException
	 */
	public Domain changeToBean() throws IOException{
		XStream stream = new XStream();
		/***********设置类别名**********/
		stream.alias("domain", Domain.class);//根标签
		stream.alias("device", Device.class);//device标签
		stream.alias("task", Task.class);//task标签
		stream.alias("taskKind", TaskKind.class);//task标签	
		/************* 设置类成员的别名 ***************/ 
		stream.aliasField("deviceList", Domain.class, "devices");//domain的device标签
		stream.aliasField("tasks", Device.class, "tasks");//device的tasks标签
		stream.aliasField("task", Device.class, "task");//tasks的task标签
		stream.aliasField("taskKind", Task.class, "taskKind");//tasks的task标签
		/******* 设置类成员为xml一个元素上的属性 *******/
		stream.useAttributeFor(Domain.class,"dmId");
		stream.useAttributeFor(Domain.class,"dmName");
		stream.useAttributeFor(Domain.class,"dmIp");
		//
		stream.useAttributeFor(Domain.class, "timeinterval");
		stream.useAttributeFor(Domain.class, "realtime");
		stream.useAttributeFor(Domain.class, "interval");
		//
		stream.useAttributeFor(Device.class,"dId");
		stream.useAttributeFor(Device.class,"dType");
		stream.useAttributeFor(Device.class,"dIp");
		stream.useAttributeFor(Device.class,"dName");
		stream.useAttributeFor(Device.class,"hardware");
		stream.useAttributeFor(Device.class,"software");
		stream.useAttributeFor(Task.class,"tId");
		stream.useAttributeFor(Task.class,"tType");
		stream.useAttributeFor(Task.class,"tState");
		stream.useAttributeFor(Task.class,"dStatus");
		stream.useAttributeFor(Task.class,"domainId");
		stream.useAttributeFor(TaskKind.class,"tkId");
		stream.useAttributeFor(TaskKind.class,"tkName");
		stream.useAttributeFor(TaskKind.class,"Oid");		
		stream.useAttributeFor(TaskKind.class,"tType");		
		/************* 设置属性的别名 ***************/ 
		stream.aliasAttribute(Domain.class, "dmId", "domainId");
		stream.aliasAttribute(Domain.class, "dmName", "domainName");
		stream.aliasAttribute(Domain.class, "dmIp", "domainIp");
		//
		stream.aliasAttribute(Domain.class, "timeinterval", "timeinterval");
		stream.aliasAttribute(Domain.class, "realtime", "realtime");
		stream.aliasAttribute(Domain.class, "interval", "interval");
		//
		stream.aliasAttribute(Device.class, "dId", "deviceId");
		stream.aliasAttribute(Device.class, "dType", "deviceType");
		stream.aliasAttribute(Device.class, "dIp", "deviceIp");
		stream.aliasAttribute(Device.class, "dName", "deviceName");
		stream.aliasAttribute(Device.class, "hardware", "hardware");
		stream.aliasAttribute(Device.class, "software", "software");
		stream.aliasAttribute(Task.class, "tId", "taskId");
		stream.aliasAttribute(Task.class, "tType", "taskType");
		stream.aliasAttribute(Task.class, "tState", "taskState");
		stream.aliasAttribute(Task.class, "dStatus", "deviceStatus");
		stream.aliasAttribute(Task.class, "domainId", "domainId");
		stream.aliasAttribute(TaskKind.class, "tkId", "taskKindId");
		stream.aliasAttribute(TaskKind.class, "tkName", "taskKindName");
		stream.aliasAttribute(TaskKind.class, "Oid", "Oid");	
		stream.aliasAttribute(TaskKind.class, "tType", "tType");	
		return (Domain) stream.fromXML(new FileInputStream("D:/init.xml"));
	}
	
	/**
	 * 读取snmp任务列表
	 */
	public Domain readTaslList() throws IOException{
		XStream stream = new XStream();
		/***********设置类别名**********/
		stream.alias("domain", Domain.class);//根标签
		stream.alias("device", Device.class);//device标签
		stream.alias("task", Task.class);//task标签
		stream.alias("taskKind", TaskKind.class);//taskKind标签	
		/************* 设置类成员的别名 ***************/ 
		stream.addImplicitCollection(Domain.class, "devices");//隐含集合
		stream.aliasField("tasks", Device.class, "tasks");//device的tasks标签
		stream.aliasField("task", Device.class, "task");//tasks的task标签
		stream.aliasField("parameter", Task.class, "taskKind");//tasks的taskKind标签
		/******* 设置类成员为xml一个元素上的属性 *******/
		stream.useAttributeFor(Domain.class,"dmId");
		stream.useAttributeFor(Domain.class,"dmIp");
		stream.useAttributeFor(Domain.class,"timeinterval");
		stream.useAttributeFor(Device.class,"dIp");
		stream.useAttributeFor(Task.class,"tId");
		stream.useAttributeFor(TaskKind.class,"tkName");
		stream.useAttributeFor(TaskKind.class,"Oid");		
		/************* 设置属性的别名 ***************/ 
		stream.aliasAttribute(Domain.class, "dmId", "domainId");
		stream.aliasAttribute(Domain.class, "dmIp", "ip");
		stream.aliasAttribute(Domain.class, "timeinterval", "timeinterval");
		stream.aliasAttribute(Device.class, "dIp", "ip");
		stream.aliasAttribute(Task.class, "tId", "taskId");
		stream.aliasAttribute(TaskKind.class, "tkName", "name");
		stream.aliasAttribute(TaskKind.class, "Oid", "value");
		return (Domain) stream.fromXML(new FileInputStream("D:/Release/1000.xml"));
	}
	
	/**
	 * 读取初始化配置文件
	 * @return
	 * @throws IOException
	 */
	public Domain BatchLoad() throws IOException{
		XStream stream = new XStream();
		/***********设置类别名**********/
		stream.alias("domain", Domain.class);//根标签
		stream.alias("device", Device.class);//device标签
		stream.alias("task", Task.class);//task标签
		stream.alias("taskKind", TaskKind.class);//task标签	
		/************* 设置类成员的别名 ***************/ 
		stream.aliasField("deviceList", Domain.class, "devices");//domain的device标签
		stream.aliasField("tasks", Device.class, "tasks");//device的tasks标签
		stream.aliasField("task", Device.class, "task");//tasks的task标签
		stream.aliasField("taskKind", Task.class, "taskKind");//tasks的task标签
		/******* 设置类成员为xml一个元素上的属性 *******/
		stream.useAttributeFor(Domain.class,"dmId");
		stream.useAttributeFor(Domain.class,"dmName");
		stream.useAttributeFor(Domain.class,"dmIp");
		//
		stream.useAttributeFor(Domain.class, "timeinterval");
		stream.useAttributeFor(Domain.class, "realtime");
		stream.useAttributeFor(Domain.class, "interval");
		//
		stream.useAttributeFor(Device.class,"dId");
		stream.useAttributeFor(Device.class,"dType");
		stream.useAttributeFor(Device.class,"dIp");
		stream.useAttributeFor(Device.class,"dName");
		stream.useAttributeFor(Device.class,"hardware");
		stream.useAttributeFor(Device.class,"software");
		stream.useAttributeFor(Task.class,"tId");
		stream.useAttributeFor(Task.class,"tType");
		stream.useAttributeFor(Task.class,"tState");
		stream.useAttributeFor(Task.class,"dStatus");
		stream.useAttributeFor(TaskKind.class,"tkId");
		stream.useAttributeFor(TaskKind.class,"tkName");
		stream.useAttributeFor(TaskKind.class,"Oid");		
		/************* 设置属性的别名 ***************/ 
		stream.aliasAttribute(Domain.class, "dmId", "domainId");
		stream.aliasAttribute(Domain.class, "dmName", "domainName");
		stream.aliasAttribute(Domain.class, "dmIp", "domainIp");
		//
		stream.aliasAttribute(Domain.class, "timeinterval", "timeinterval");
		stream.aliasAttribute(Domain.class, "realtime", "realtime");
		stream.aliasAttribute(Domain.class, "interval", "interval");
		//
		stream.aliasAttribute(Device.class, "dId", "deviceId");
		stream.aliasAttribute(Device.class, "dType", "deviceType");
		stream.aliasAttribute(Device.class, "dIp", "deviceIp");
		stream.aliasAttribute(Device.class, "dName", "deviceName");
		stream.aliasAttribute(Device.class, "hardware", "hardware");
		stream.aliasAttribute(Device.class, "software", "software");
		stream.aliasAttribute(Task.class, "tId", "taskId");
		stream.aliasAttribute(Task.class, "tType", "taskType");
		stream.aliasAttribute(Task.class, "tState", "taskState");
		stream.aliasAttribute(Task.class, "dStatus", "deviceStatus");
		stream.aliasAttribute(TaskKind.class, "tkId", "taskKindId");
		stream.aliasAttribute(TaskKind.class, "tkName", "taskKindName");
		stream.aliasAttribute(TaskKind.class, "Oid", "Oid");	
		return (Domain) stream.fromXML(new FileInputStream("D:/batchLoad.xml"));
	}
	
	public static void main(String[] args) {
		TurnXmlToDevice deviceToXml = new TurnXmlToDevice();
		try {
			//Domain domain = deviceToXml.readTaslList();
			//System.out.println(domain.getTimeinterval());
			Domain domain = deviceToXml.BatchLoad();
			List<Device> deviceList = domain.getDevices();
			List<Task> taskList = new ArrayList<Task>();
			int taskNum = 0;
			for (Iterator iterator = deviceList.iterator(); iterator.hasNext();) {
				Device device = (Device) iterator.next();
				taskList = device.getTasks();
				taskNum = taskList.size()+taskNum;
			}
			System.out.println("任务数："+taskNum);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
